
// 创建路由
const express =require('express')
const jwt =require('jsonwebtoken')
const router = express.Router();
const sqlFun =require('./mysql')
//路由走向
//登入接口
router.post('/login',(req,res)=>{
    // 获取用户输入的内容
    let user =req.body.username
    let pwd = req.body.password
    //sql查询当前输入的账号密码是否在正缺
    let sql=`select * from userinfo where username='${user}' and password='${pwd}'` 
    sqlFun(sql,result=>{
      // console.log(result);
      if(result.length>0){
        const token = jwt.sign({
          uid:result[0].id,
          user,
          xx:'xx'
      },"jwtsomekeys")
        res.send({
          msg:'登入成功',
          status:200,
          token,
          user
        })
      }else{
        res.status(402).send({
            msg:'',
            info:'账号密码错误'
        })
      }
    })
    // res.send('登入车和改革')
})
// 注册验证接口
router.post('/verRegist',(req,res)=>{
  let user=req.body.username
  // 查询账号是否存在
  let sql=`select * from userinfo where username='${user}'`
  sqlFun(sql,result=>{
    // 判断返回的结果
    if(result.length>0){
        res.send({
          info:'账号一存在',
          msg:false
        })
    }else{
      res.send({
        info:'账号不存在',
        msg:true
      })
    }
  })
})
// 注册接口
router.post('/register',(req,res)=>{
  let user=req.body.username
  let pwd =req.body.password
  //sql语句，插入语句
  let sql=`insert into userinfo(username,password) values('${user}','${pwd}')`
  //执行sql
  sqlFun(sql,result=>{
    // console.log(result)
    if(result.affectedRows>0){
      res.send({
        success:true,
        status:200,

      })
    }else{
      res.send({
        success:false,
        status:500
      })
    }
  })
})
module.exports =router